package br.com.gustavobhz.agenda.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.gustavobhz.mvc.logica.Logica;

public class ControllerServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
    }
	 
	public void service(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		
		String parametro = request.getParameter("logica");
		String nomeDaClasse = "br.com.gustavobhz.mvc.logica." + parametro;
		
		try{
			Class classe = Class.forName(nomeDaClasse);
			
			Logica logica = (Logica) classe.newInstance();
			
			logica.executa(request, response);
			
		} catch (Exception e) {
			throw new ServletException("A lógica de " + "negócios causou uma exceção", e);
		}
	}

}